package cn.neusoft.dao.impl;

import cn.neusoft.bean.Order;
import cn.neusoft.dao.IOrderDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.OrderQuery;
import cn.neusoft.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDao implements IOrderDao {
    private QueryRunner queryRunner;

    public OrderDao(){
        queryRunner = JdbcUtils.getQueryRunner();
    }

    @Override
    public int add(Order order) throws SQLException {
        String sql = "insert into t_orders (userId, businessId, orderDate,orderTotal,daId,orderState) values(?,?,?,?,?,?)";
        return queryRunner.update(sql,
                order.getUserId(),
                order.getBusinessId(),
                order.getOrderDate(),
                order.getOrderTotal(),
                order.getDaId(),
                order.getOrderState());
    }

    @Override
    public int update(Order order) throws SQLException {
        String sql = "update t_orders set userId=?, businessId=?, orderDate=?,orderTotal=?,daId=?,orderState=? where orderId=?";
        return queryRunner.update(sql,
                order.getUserId(),
                order.getBusinessId(),
                order.getOrderDate(),
                order.getOrderTotal(),
                order.getDaId(),
                order.getOrderState(),
                order.getOrderId());
    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from t_orders where orderId=?";
        return queryRunner.update(sql,id);
    }

    @Override
    public Order getById(Integer id) throws SQLException {
        String sql = "select orderId,userId, businessId, orderDate,orderTotal,daId,orderState from t_orders where orderId=?";
        return queryRunner.query(sql, new BeanHandler<Order>(Order.class), id);
    }

    @Override
    public List<Order> getAll() throws SQLException {
        String sql = "select orderId,userId, businessId, orderDate,orderTotal,daId,orderState from t_orders";
        return queryRunner.query(sql, new BeanListHandler<Order>(Order.class));
    }

    @Override
    public int getCount(BaseQuery baseQuery) throws SQLException {
        OrderQuery orderQuery =(OrderQuery) baseQuery;
        List<Object> params = new ArrayList<>();
        String sql="select count(orderId) from t_orders where 1=1";
        if (orderQuery.getUserId()!=null){
            sql+=" and userId like concat('%',?,'%')";
            params.add(orderQuery.getUserId());
        }
        System.out.println("查询集合："+sql);
        System.out.println("参数集合："+params);

        return queryRunner.query(sql, new ScalarHandler<Long>(),params.toArray()).intValue();
    }

    @Override
    public List<Order> getByPage(BaseQuery baseQuery) throws SQLException {
        OrderQuery orderQuery =(OrderQuery) baseQuery;
        List<Object> params = new ArrayList<>();
        String sql="select orderId,userId, businessId, orderDate,orderTotal,daId,orderState from t_orders where 1=1";
        if (orderQuery.getUserId()!=null){
            sql+=" and userId like concat('%',?,'%')";
            params.add(orderQuery.getUserId());
        }
        sql+=" order by orderId desc limit ?,?";
        params.add(orderQuery.getStartIndex());
        params.add(orderQuery.getPageCount());
        System.out.println("查询集合："+sql);
        System.out.println("参数集合："+params);

        return queryRunner.query(sql,new BeanListHandler<Order>(Order.class),params.toArray());
    }
}
